/**
 * Created by zhouqian on 2017/4/24.
 */
var DB =  require('../db/db');
var IncreaseId = require('../tools/IncreaseId');

/**结算(添加商品信息到 k_order_goods表中)*/
exports.addOrderGoods = function(req,res,msg){
    var id = IncreaseId.increaseId();
    var goods_id = req.body.goods_id;
    var goods_name = req.body.goods_name;
    var goods_price = req.body.goods_price;
    var goods_number = req.body.goods_number;
    var good_type = req.body.goods_type;
    var goods_attr = req.body.goods_attr!="undefined"?req.body.goods_attr:0;
    var shop_id = req.body.shop_id!="undefined"?req.body.shop_id:"";
    var good_tax_fee = req.body.good_tax_fee;
    var order_id =req.body.order_id;
    var is_discount =req.body.is_discount;
    var sql = "INSERT into k_order_goods(id,order_id,goods_id,goods_name,goods_price,goods_number,good_type,goods_attr,shop_id,good_tax_fee,is_discount) VALUES ("
        +"'"+ id +"',"
        +"'"+ order_id +"',"
        +"'"+ goods_id +"',"
        +"'"+ goods_name +"',"
        +"'"+ goods_price +"',"
        +"'"+ goods_number +"',"
        +"'"+ good_type +"',"
        +"'"+ goods_attr +"',"
        +"'"+ shop_id +"',"
        +"'"+ good_tax_fee +"',"
        +"'"+ is_discount +"')";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};

/**结算(添加商品信息到 k_order_info表中)*/
exports.addOrderInfo = function(req,res,msg){
    var orderId = req.body.orderId;
    var order_sn = req.body.order_sn;
    var user_id = req.body.user_id;
    var order_status = req.body.order_status;
    var shipping_status = req.body.shipping_status;
    var pay_status = req.body.pay_status;
    var goods_amount = req.body.goods_amount;
    var shipping_fee = req.body.shipping_fee;
    var order_amount = req.body.order_amount;
    var goods_tax = req.body.goods_tax;
    var add_time = req.body.add_time;
    var country = "中国";
    var order_point = req.body.order_point?req.body.order_point:0;
    var is_point_order = order_point !=0 ? 1 : 0;
    var sql = "INSERT into k_order_info(id,country,order_sn,user_id,order_status,shipping_status,pay_status,goods_amount,shipping_fee,order_amount,goods_tax,add_time,is_point_order,order_point) VALUES ('"
        + orderId +"',"
        +"'"+ country +"',"
        +"'"+ order_sn +"',"
        +"'"+ user_id +"',"
        +"'"+ order_status +"',"
        +"'"+ shipping_status +"',"
        +"'"+ pay_status +"',"
        +"'"+ goods_amount +"',"
        +"'"+ shipping_fee +"',"
        +"'"+ order_amount +"',"
        +"'"+ goods_tax +"',"
        +"'"+ add_time +"',"
        +"'"+ is_point_order +"',"
        +"'"+ order_point +"')";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};
/**获取订单折扣商品信息*/
exports.getZhezhouPro = function(req,res,msg){
    var user_id = req.body.user_id;
    var goods_id = req.body.goods_id;
    var shop_id = req.body.shop_id;
    var is_discount = req.body.is_discount;
    var sql = "SELECT o.* FROM k_order_info as o"
        + " LEFT JOIN k_order_goods as og ON o.id = og.order_id "
        + " LEFT JOIN k_shop as s ON og.shop_id = s.id"
        + " WHERE o.user_id ='"+user_id+"' AND og.is_discount='"+is_discount+"' AND og.goods_id='"+goods_id+"' AND og.shop_id='" + shop_id + "'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};
/**根据userId获取订单信息*/
exports.getOrderByUserId = function(req,res,msg){
    var user_id = req.body.userId;
    var orderId = req.body.orderId;
    if(orderId){
        var sql = "SELECT s.shopname,g.id,g.goods_name,g.goods_img,g.goods_desc,g.good_tax_rate,og.goods_id,og.goods_sn,og.goods_price,og.goods_number,o.* FROM k_order_info as o " +
            " LEFT JOIN k_order_goods as og ON o.id = og.order_id " +
            " LEFT JOIN k_goods as g ON og.goods_id = g.id"+
            " LEFT JOIN k_shop as s ON og.shop_id = s.id"+
            " WHERE o.user_id ='"+user_id+"' AND o.id='"+orderId+"' ORDER BY o.add_time DESC";
    }else{
        var sql = "SELECT * FROM k_order_info WHERE user_id ='"+user_id+"' ORDER BY add_time DESC";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};
/**根据orderId获取订单商品信息*/
exports.getOrderGoods = function(req,res,msg){
    var order_id = req.body.order_id;
    if(order_id) {
        var sql = "SELECT s.shopname,g.id,g.goods_name,g.goods_img,g.goods_desc,g.good_tax_rate,og.order_id,og.goods_id,og.goods_sn,og.goods_price,og.goods_number FROM k_order_goods as og " +
            " LEFT JOIN k_goods as g ON og.goods_id = g.id" +
            " LEFT JOIN k_shop as s ON og.shop_id = s.id"+
            " WHERE og.order_id ='" + order_id + "'";
        console.log(sql);
        DB.query(sql, function (er, list) {
            if (!er) {
                msg.success = true;
                msg.data = list;
                res.json(msg);
            } else {
                msg.msg = er;
                res.json(msg);
            }
        });
    }else {
        msg.msg = er;
        res.json(msg);
    }
};

/**判断订单是否免邮*/
exports.getShipping = function(req,res,msg){
    var order_id = req.body.order_id;
    var sql = "select count(g.is_shipping) as shipping from k_goods as g LEFT JOIN k_order_goods as og on g.id=og.goods_id WHERE og.order_id='"+order_id+"'";
    console.log(sql);
    DB.query(sql, function (er, list) {
        if (!er) {
            if(list[0].shipping>0){
                var list1 = {shipping_money:0};
                msg.success = true;
                msg.data = list1;
                res.json(msg);
            }else{
                var sql1 ="select shipping_money FROM k_shipping";
                console.log(sql1);
                DB.query(sql1, function (er, list) {
                    if (!er) {
                        msg.success = true;
                        msg.data = list[0];
                        res.json(msg);
                    }
                });
            }
        } else {
            msg.msg = er;
            res.json(msg);
        }
    });
};

/**更新订单收货地址*/
exports.UpdateAddressToOrderInfo = function(req,res,msg){
    var user_id = req.body.user_id;
    var orderId = req.body.orderId;
    var addressInfo = JSON.parse(req.body.addressInfo);
    var order_comment = req.body.order_comment;
    if(order_comment){
        var sql = "UPDATE k_order_info SET " +
            "consignee ='"+addressInfo.realname+"'," +
            "province ='"+addressInfo.province+"'," +
            "city ='"+addressInfo.city+"'," +
            "district ='"+addressInfo.district+"'," +
            "address ='"+addressInfo.address+"'," +
            "zipcode ='"+addressInfo.zipcode+"'," +
            "mobile ='"+addressInfo.mobile+"'," +
            "postcard ='"+addressInfo.idnumber+"'," +
            "order_comment ='"+order_comment+"'" +
            " WHERE user_id='"+ user_id +"' AND id='"+orderId+"'";
    }else{
        var sql = "UPDATE k_order_info SET " +
            "consignee ='"+addressInfo.realname+"'," +
            "province ='"+addressInfo.province+"'," +
            "city ='"+addressInfo.city+"'," +
            "district ='"+addressInfo.district+"'," +
            "address ='"+addressInfo.address+"'," +
            "zipcode ='"+addressInfo.zipcode+"'," +
            "mobile ='"+addressInfo.mobile+"'," +
            "postcard ='"+addressInfo.idnumber+"'" +
            " WHERE user_id='"+ user_id +"' AND id='"+orderId+"'";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};
/**更新订单支付信息*/
exports.UpdatePay = function(req,res,msg){
    var pay_status = req.body.pay_status;
    var order_sn = req.body.order_sn;
    var pay_time = req.body.pay_time;
    var pay_trade_no = req.body.pay_trade_no;
    var sql = "UPDATE k_order_info SET " +
        "pay_status ='"+pay_status+"'," +
        "pay_time ='"+pay_time+"'," +
        "pay_trade_no ='"+pay_trade_no+"'" +
        " WHERE order_sn='"+ order_sn +"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};
/**确认收货*/
exports.updateShippingStatus = function(req,res,msg){
    var id = req.body.id;
    var user_id = req.body.user_id;
    var shipping_status = req.body.shipping_status;
    var sql = "UPDATE k_order_info SET " +
        "shipping_status ='"+shipping_status+"'" +
        " WHERE id ='"+ id +"' AND user_id ='"+user_id+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};
/**通过订单ID获取物流信息*/
exports.getLogisticsByOrderid = function(req,res,msg){
    var id = req.body.id;
    var sql = "SELECT shipping_name,shipping_num FROM k_order_info WHERE id ='"+ id +"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};

/**取消订单*/
exports.deleteOrderInfo = function(req,res,msg){
    var id = req.body.id;
    var user_id = req.body.user_id;
    var sql = "DELETE o.*,og.* FROM k_order_info as o LEFT JOIN k_order_goods as og ON o.id = og.order_id" +
        " WHERE o.id ='"+ id +"' AND o.user_id ='"+user_id+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};